
/**
 * 部门管理初始化
 */
var Dept = {
    id: "DeptTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
/**
 * 初始化表格的列
 */
Dept.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
       /* {title: 'id', field: 'id', align: 'center', valign: 'middle',sortable: true,visible: false}, */
        {title: '部门名称', field: 'fullname', align: 'left', valign: 'middle', sortable: true},//,width:'50px'
        {title: '部门简称', field: 'simplename', align: 'left', valign: 'middle', sortable: true},       
        {title: '上级部门', field: 'pName', align: 'center', valign: 'middle'},
        {title: '所属机构', field: 'officeName', align: 'center', valign: 'middle'},
        {title: '备注', field: 'tips', align: 'center', valign: 'middle', }];
};

/**
 * 检查是否选中
 */
Dept.check = function () {
    var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Dept.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加部门
 */
Dept.openAddDept = function () {
    var index = layer.open({
        type: 2,
        title: '添加部门',
        area: ['100%', '100%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/sys/sysDept/add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看部门详情
 */
Dept.openDeptDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改部门',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/sys/sysDept/update/' + Dept.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 搜索重置
 */
Dept.resetSearch = function () {
    $("#condition").val("");
    Dept.search();
}

/**
 * 删除部门
 */
Dept.delete = function () {
    if (this.check()) {
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/sys/sysDept/save/delete", function (data) {
            	if(data.resultCode == 1){
            		Feng.success("删除成功!");
                    Dept.table.refresh();
            	}else{
            		Feng.error("删除失败!" + data.msg+ "!");
            	}
                
               /* //销毁子模型
                if(DeptChildren.table){
                	DeptChildren.table.destroy();
                	$("#DeptTableChildren").html("");
                }*/
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("deptId",Dept.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否刪除该部门及下级部门?", operation);
    }
};

/**
 * 查询部门列表
 */
Dept.search = function () {
	var condition = $("#condition").val();
	var officeId = $("#officeId").val();
    var queryData = {
    		"fullname":condition,
    		"officeId":officeId
    };
    Dept.table.refresh({query: queryData});
};

Dept.clickOfficeTree = function(e, treeId, treeNode){
	var officeId = treeNode.id;
	$("#officeId").val(officeId);
	Dept.search();
}


$(function () {
    var defaultColunms = Dept.initColumn();
    var paramsId =[{"key":"officeId","type":"input","id":"officeId"}];
    var table = new BSTreeTable(Dept.id, "/sys/sysDept/list", defaultColunms,paramsId);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("pid");
    table.setExpandAll(true);
    table.init();
    Dept.table = table;
    
    
    
    var ztree = new $ZTree("officeTree", "/sys/sysOffice/tree/-1");
    ztree.bindOnClick(Dept.clickOfficeTree);
    ztree.init();  
});


